package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("StoremethodDAO")
@Transactional
public class StoremethodDAOImpl extends AbstractJpaDao<Storemethod> implements StoremethodDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Storemethod.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public StoremethodDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Storemethod findStoremethodById(Integer id) throws DataAccessException {

		return findStoremethodById(id, -1, -1);
	}

	@Transactional
	public Storemethod findStoremethodById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStoremethodById", startResult, maxRows, id);
			return (Storemethod) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Storemethod> findStoremethodByName(String name) throws DataAccessException {

		return findStoremethodByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Storemethod> findStoremethodByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStoremethodByName", startResult, maxRows, name);
		return new LinkedHashSet<Storemethod>(query.getResultList());
	}

	@Transactional
	public Set<Storemethod> findStoremethodByNameContaining(String name) throws DataAccessException {

		return findStoremethodByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Storemethod> findStoremethodByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStoremethodByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Storemethod>(query.getResultList());
	}

	@Transactional
	public Set<Storemethod> findStoremethodByDescription(String description) throws DataAccessException {

		return findStoremethodByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Storemethod> findStoremethodByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStoremethodByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Storemethod>(query.getResultList());
	}

	@Transactional
	public Set<Storemethod> findStoremethodByDescriptionContaining(String description) throws DataAccessException {

		return findStoremethodByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Storemethod> findStoremethodByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStoremethodByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Storemethod>(query.getResultList());
	}

	@Transactional
	public Storemethod findStoremethodByPrimaryKey(Integer id) throws DataAccessException {
		return findStoremethodByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Storemethod findStoremethodByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findStoremethodByPrimaryKey", startResult, maxRows, id);
			return (Storemethod) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Storemethod> findStoremethodsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStoremethodsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Storemethod> findAllStoremethods() throws DataAccessException {

		return findAllStoremethods(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Storemethod> findAllStoremethods(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllStoremethods", startResult, maxRows);
		return new LinkedHashSet<Storemethod>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Storemethod> findStoremethodByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Storemethod>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Storemethod> findStoremethodsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Storemethod.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Storemethod> storemethods = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return storemethods;
	}

	@Transactional
	public Storemethod findStoremethodByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findStoremethodByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Storemethod findStoremethodByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findStoremethodByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Storemethod) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Storemethod entity) {
		return true;
	}
}
